/**
 * Copyright (C) 2011 Taobao Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.taobao.jaket;

import junit.framework.Assert;

import org.junit.Test;

import com.google.gson.Gson;
import com.taobao.jaket.model.PersonWithRef;

/**
 * @author Xueqiang Mi
 */
public class JsonRendererTest_ObjectWithRef {

    @Test
    public void testObjectWithRef1() {
        JsonRenderer renderer = new JsonRenderer();
        String json = renderer.name("id").value(34780).name("name").value("yijiang").name("self").refSelf().name("age")
                .value(25).build();
        System.out.println(json);

        PersonWithRef newPerson = new Gson().fromJson(json, PersonWithRef.class);
        Assert.assertEquals(34780, newPerson.getId());
        Assert.assertEquals("yijiang", newPerson.getName());
        Assert.assertEquals(25, newPerson.getAge());
    }

    @Test
    public void testObjectWithRef11() {
        PersonWithRef p = new PersonWithRef();
        p.setId(34780);
        p.setName("yijiang");
        p.setSelf(p);

        JsonRenderer renderer = new JsonRenderer();
        String json = renderer.name("id").value(34780).name("name").value("yijiang").name("self").valueWithRef(p).name(
                "age").value(25).build();
        System.out.println(json);

        PersonWithRef newPerson = new Gson().fromJson(json, PersonWithRef.class);
        Assert.assertEquals(34780, newPerson.getId());
        Assert.assertEquals("yijiang", newPerson.getName());
        Assert.assertEquals(25, newPerson.getAge());
    }

    @Test
    public void testObjectWithRef12() {
        PersonWithRef p = new PersonWithRef();
        p.setId(00001);
        p.setName("00001");
        p.setSelf(p);

        PersonWithRef p1 = new PersonWithRef();
        p1.setId(00002);
        p1.setName("00002");
        p1.setSelf(p);

        PersonWithRef p2 = new PersonWithRef();
        p2.setId(00003);
        p2.setName("00003");
        p2.setSelf(p);

        JsonRenderer renderer = new JsonRenderer();
        String json = renderer.name("p1").valueWithRef(p1).name("p2").valueWithRef(p2).build();
        System.out.println(json);
    }
}
